﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using camcad7Home.CultureHelper;
namespace camcad7Home.Controllers
{
    public class LanguageController : Controller
    {
        //
        // GET: /Language/

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult SetLanguage(string lang,string returnUrl)
        {
            lang = camcad7Home.CultureHelper.CultureHelper.GetValidCulture(lang);

            lang = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            HttpCookie cultureCookie;
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());

            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            }
            Response.Cookies.Add(cultureCookie);
            if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        [HttpPost]
        public ActionResult AjaxLang(string lang, string itemid)
        {
           
            lang = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            HttpCookie cultureCookie;
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());

            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];
                cultureCookie["lang"] = CultureHelper.CultureHelper.GetValidCulture(lang.ToString());
            }
            Response.Cookies.Add(cultureCookie);
            string newurl = "";
            if (itemid != null)
            {
                   Cadcam7Moddel.Context.DataContext db = new Cadcam7Moddel.Context.DataContext();
                    string a = Request.Cookies["_culture"]["lang"];
                    int itemids = int.Parse(itemid);
                    var page = db.Trang.Where(m => m.ItemID == itemids&& m.LangID == a).FirstOrDefault();
                   if(page!=null)
                   {
                       newurl = Url.Action("xemtintuc", "Trang", new { tieude = Cadcam7Moddel.Model.Conver.ConvertToUnSign(page.Tieude), id = page.ID });
                   }
               
            }
            return Json(new {sucess=true,url=newurl});
        }
    }
}
